import requests
from bs4 import BeautifulSoup
def get_cityid(city):
    url = 'http://toy1.weather.com.cn/search'
    header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.39 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.39'}
    params = {
        'cityname':city
    }
    response = requests.get(url,headers=header,params=params)
    citys =response.content.decode(('utf-8'))
    if city in citys:
        start = citys.find(':')
        end = citys.find('~')
        cityid = citys[start+2:end]
    else:
        cityid = False
    return cityid
def spider_1dweather(city):
    cityid = get_cityid(city)
    if cityid:
        url = 'http://www.weather.com.cn/weather1d/' + str(cityid) + '.shtml'
        header = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.39 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.39'}
        response = requests.get(url, headers=header)
        html = response.content.decode('utf-8')
        soup = BeautifulSoup(html,'lxml')
        weather_info = {
            'title':None,
            'wea':None,
            'tem':None,
            'win':None,
            'sun':None
        }
        weather = {
            'daytime':weather_info,
            'night':weather_info.copy()
        }
        div_tags = soup.find_all('div',attrs={'class':'t'})[0]
        weather['daytime']['title'] = div_tags.find_all('h1')[0].get_text()
        weather['daytime']['wea'] = div_tags.find_all('p',attrs={'class':'wea'})[0].get_text()
        weather['daytime']['tem'] = div_tags.find_all('p', attrs={'class': 'tem'})[0].get_text().replace('\n','')
        weather['daytime']['win'] = div_tags.find_all('p', attrs={'class': 'win'})[0].get_text().replace('\n','')
        weather['daytime']['sun'] = div_tags.find_all('p', attrs={'class': 'wea'})[0].get_text().replace('\n','')
        weather['night']['title'] = div_tags.find_all('h1')[1].get_text()
        weather['night']['wea'] = div_tags.find_all('p', attrs={'class': 'wea'})[1].get_text()
        weather['night']['tem'] = div_tags.find_all('p', attrs={'class': 'tem'})[1].get_text().replace('\n','')
        weather['night']['win'] = div_tags.find_all('p', attrs={'class': 'win'})[1].get_text().replace('\n','')
        weather['night']['sun'] = div_tags.find_all('p', attrs={'class': 'wea'})[0].get_text().replace('\n','')
        return weather
    else:
        return False
def spider_7dweather(city):
    cityid = get_cityid(city)
    if cityid:
        url = 'http://www.weather.com.cn/weather/' + str(cityid) +'.shtml'
        header = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.39 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.39'}
        response = requests.get(url, headers=header)
        html = response.content.decode('utf-8')
        soup = BeautifulSoup(html,'lxml')
        weather_info = {
            'title': None,
            'wea': None,
            'tem': None,
            'win': None
        }
        weather = dict()
        for i in range(7):
            weather[str(i)] = weather_info.copy()
        ul_tag = soup.find_all('ul',attrs={'class':'t clearfix'})[0]
        for i in range(7):
            li_tag = ul_tag.find_all('li')[i]
            weather[str(i)]['title'] = li_tag.h1.get_text()
            weather[str(i)]['wea'] = li_tag.find_all('p')[0].get_text().replace('\n','')
            weather[str(i)]['tem'] = li_tag.find_all('p')[1].get_text().replace('\n','')
            weather[str(i)]['win'] = li_tag.find_all('p')[2].get_text().replace('\n','')
        return weather
    else:
        return False